﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Besture.Bll;
using Besture.Dal;
using Besture.Model;
using Besture.Common;

namespace Besture.UI.Portal.Hotel
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        public static string Img_Path = "/Resource/images/";

        public string hotelID = "";
        public string bt = DateTime.Now.ToString("yyyy-MM-dd");
        public string et = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
        public string endTime = "";
        public string userImg = "../../Resource/images/head.jpg";
        public string userLoginName = "匿名用户";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["hotelid"] != null && Request.QueryString["bt"] != null && Request.QueryString["et"] != null)
            {
                if (StringHelper.IsDateTime(Request.QueryString["bt"].ToString()))
                {
                    bt = Request.QueryString["bt"].ToString();
                }
                if (StringHelper.IsDateTime(Request.QueryString["et"].ToString()))
                {
                    et = Request.QueryString["et"].ToString();
                }
                if (StringHelper.IsNumber(Request.QueryString["hotelid"].ToString()))
                {
                    hotelID = Request.QueryString["hotelid"].ToString();
                }
                else
                {
                    Response.Redirect("Product.aspx");
                }
            }
            else
            {
                Response.Redirect("Product.aspx");
            }

            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel ubm = ((UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL]);
                userImg = ubm.UserImage;
                userLoginName = ubm.LoginName;
            }
        }

        /// <summary>
        /// 查询酒店详情
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [WebMethod]
        public static string GetHotelsByID(int hotelid)
        {
            string json = "";
            StringBuilder sb = new StringBuilder();
            HotelsDac dac = new HotelsDac();
            DataSet ds = dac.GetHotelsByID(hotelid);
            IList<HotelsModel> listHotel = ModelConvertHelper<HotelsModel>.ConvertToModelList(ds.Tables[0]);
            IList<ImageModel> listImage = ModelConvertHelper<ImageModel>.ConvertToModelList(ds.Tables[1]);
            IList<TrafficModel> listTraffic = ModelConvertHelper<TrafficModel>.ConvertToModelList(ds.Tables[2]);
            IList<HotelDetail_Advisory_Model> listAdvisory = ModelConvertHelper<HotelDetail_Advisory_Model>.ConvertToModelList(ds.Tables[3]);

            sb.Append("{");
            sb.Append("\"ID\":\"" + listHotel[0].ID + "\",");
            sb.Append("\"HotelsName\":\"" + listHotel[0].HotelsName + "\",");
            sb.Append("\"HotelsImage\":\"" + listHotel[0].HotelsImage + "\",");
            sb.Append("\"Address\":\"" + listHotel[0].Address + "\",");
            sb.Append("\"Tel\":\"" + listHotel[0].Tel + "\",");
            sb.Append("\"Longitude\":\"" + listHotel[0].Longitude + "\",");
            sb.Append("\"Latitude\":\"" + listHotel[0].Latitude + "\",");
            sb.Append("\"Detail\":\"" + listHotel[0].Detail.Replace("\"","\\\"") + "\",");
            sb.Append("\"NetworkFacilities\":\"" + listHotel[0].NetworkFacilities.Replace("\"", "\\\"") + "\",");
            sb.Append("\"ParkingLot\":\"" + listHotel[0].ParkingLot.Replace("\"", "\\\"") + "\",");
            sb.Append("\"HotelFacilities\":\"" + listHotel[0].HotelFacilities.Replace("\"", "\\\"") + "\"");
            sb.Append("}");
            json += "{\"items\":[" + sb.ToString() + "]},";
            sb.Clear();
            foreach (ImageModel m in listImage)
            {
                sb.Append("{");
                sb.Append("\"ImageUrl\":\"" + Constants.PATH_IMAGE_CONTENT + m.ImageUrl + "\"");
                sb.Append("},");
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            sb.Clear();
            foreach (TrafficModel m in listTraffic)
            {
                sb.Append("{");
                sb.Append("\"Location\":\"" + m.Location + "\",");
                sb.Append("\"GettingThere\":\"" + m.GettingThere + "\"");
                sb.Append("},");
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            sb.Clear();
            foreach (HotelDetail_Advisory_Model m in listAdvisory)
            {
                sb.Append("{");
                sb.Append("\"NickName\":\"" + m.NickName.Substring(m.NickName.Length / 2) + "***" + "\",");
                sb.Append("\"AskTime\":\"" + m.AskTime.ToString("yyyy-MM-dd") + "\",");
                sb.Append("\"Content\":\"" + m.Content + "\",");
                sb.Append("\"ReplyName\":\"" + m.ReplyName.Substring(m.ReplyName.Length / 2) + "***" + "\",");
                sb.Append("\"ReplyTime\":\"" + m.ReplyTime.ToString("yyyy-MM-dd") + "\",");
                sb.Append("\"Reply\":\"" + m.Reply + "\"");
                sb.Append("},");
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]}";
            return "[" + json + "]";
        }

        /// <summary>
        /// 根据酒店ID查询房态
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [WebMethod]
        public static string GetRoomByHotelID(int hotelid, string begintime, string endtime)
        {
            int userType = 0;

            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel model = (UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL];
                userType = model.UserType;
            }

            StringBuilder sb = new StringBuilder();
            HotelsDac dac = new HotelsDac();
            DataSet ds = dac.GetRoomByHotelID(userType, hotelid, begintime, endtime);
            IList<HotelList_HotelRoom_Model> listRoom = ModelConvertHelper<HotelList_HotelRoom_Model>.ConvertToModelList(ds.Tables[0]);
            IList<HotelList_RoomImage_Model> listImage = ModelConvertHelper<HotelList_RoomImage_Model>.ConvertToModelList(ds.Tables[1]);

            if (listRoom.Count > 0)
            {
                string lowprice = listRoom.Min(p => p.RoomPrice).ToString("f0");

                foreach (HotelList_HotelRoom_Model m in listRoom)
                {
                    sb.Append("<tr>");
                    sb.Append("<td><img alt=\"\" style=\"width:50px; height:50px;\" src=\"" + Constants.PATH_IMAGE_CONTENT + m.RoomImage + "\" /><br /><a href=\"javascript:;\" class=\"room_type\">" + m.RoomName + "</a></td>");
                    sb.Append("<td>" + m.BedType + "</td>");
                    sb.Append("<td>" + m.Breakfast + "</td>");
                    sb.Append("<td>" + m.Broadband + "</td>");
                    sb.Append("<td><p class=\"price\">￥<span>" + m.RoomPrice.ToString("f0") + "</span></p></td>");
                    if (m.HousingStateID == 3)
                    {
                        sb.Append("<td><a href=\"javascript:alert('房间已满！');\" class=\"btn disable\">预定</a> <span class=\"btn_tip\"><img alt=\"\" src=\"" + Img_Path + "tipico.png\" />" + m.HousingState + "</span></td>");
                    }
                    else
                    {
                        sb.Append("<td><a href=\"javascript:RedirectPay('" + hotelid.ToString() + "/" + m.RoomID.ToString() + "');\"" + " class=\"btn\">预定</a> <span class=\"btn_tip\"><img alt=\"\" src=\"" + Img_Path + "tipico.png\" />" + m.HousingState + "</span></td>");
                    }
                    sb.Append("</tr>");
                    sb.Append("<tr>");
                    sb.Append("<td colspan=\"6\"><ul>");
                    sb.Append("<li>");
                    IList<HotelList_RoomImage_Model> listImage1 = listImage.Where(p => p.RoomID == m.RoomID).ToList();
                    foreach (HotelList_RoomImage_Model m1 in listImage1)
                    {
                        sb.Append("<img alt=\"\" style=\"width:75px; height:75px;\" src=\"" + Constants.PATH_IMAGE_CONTENT + m1.ImageUrl + "\" /> ");
                    }
                    sb.Append("</li>");
                    sb.Append("<li>建筑面积：" + m.RoomArea.ToString() + "平方米</li>");
                    sb.Append("<li>楼层：" + m.Floor.ToString() + "层</li>");
                    sb.Append("<li>床型：" + m.BedType + "</li>");
                    sb.Append("<li>无窗无烟房：" + m.Smoke + "</li>");
                    sb.Append("<li>最多入住人数：" + m.PeopleNumber.ToString() + "人</li>");
                    sb.Append("</ul><a href=\"#\" class=\"slideup\">收起</a></td>");
                    sb.Append("</tr>");
                }

                return lowprice + "," + sb.ToString();
            }
            else
            {
                return ",";
            }
        }

        /// <summary>
        /// 酒店咨询
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string AddAdvisoryByHotelID(int hotelid, string content)
        {
            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel ubm = ((UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL]);
                RelatedAdvisoryDac dac = new RelatedAdvisoryDac();
                RelatedAdvisoryModel model = new RelatedAdvisoryModel();
                model.HotelsID = hotelid;
                model.UserID = ubm.ID;
                model.Content = content;
                model.Reply = "";
                model.ReplyAdminUserID = 0;
                model.ReplyTime = DateTime.Now;
                model.SortID = 0;
                model.IsDeleted = 0;
                model.Created = ubm.ID;
                model.CreateTime = DateTime.Now;
                model.Updated = ubm.ID;
                model.UpdateTime = DateTime.Now;
                int result = dac.Add(model);
                return result == -1 ? "-1" : "1";
            }
            else
            {
                return "0";
            }
        }

        /// <summary>
        /// 根据酒店ID查询评价
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string GetAppraisalByHotelID(int hotelid, int pageindex)
        {
            string total = "";
            string score = "";
            StringBuilder sb = new StringBuilder();
            ReviewsReplyDac dac = new ReviewsReplyDac();
            DataSet ds = dac.GetAppraisalByHotelID(hotelid, pageindex, out total, out score);
            IList<HotelDetail_Appraisal_Model> listAppraisal = ModelConvertHelper<HotelDetail_Appraisal_Model>.ConvertToModelList(ds.Tables[0]);
            IList<HotelDetail_ReviewsReply_Model> listReviewsReply = ModelConvertHelper<HotelDetail_ReviewsReply_Model>.ConvertToModelList(ds.Tables[1]);

            foreach (HotelDetail_Appraisal_Model model in listAppraisal)
            {
                sb.Append("<div class=\"item\">");
                sb.Append("<a href=\"#\" class=\"userimg\">");
                sb.Append("<img src=\"" + Constants.PATH_IMAGE_CONTENT + model.UserImage + "\" style=\"width: 53px\"/><br />");
                sb.Append(model.LoginName);
                sb.Append("</a>");
                sb.Append("<ul>");
                sb.Append("<li>");
                sb.Append("<div class=\"fleft\">");
                sb.Append("<b>评分：</b>");
                sb.Append("<img src=\"../../Resource/images/star.gif\" style=\"margin: 0px 0px 5px -10px;\" />");
                sb.Append("</div>");
                sb.Append("<span class=\"fright\">评论时间：" + model.AppraisalTime + "</span>");
                sb.Append("<br class=\"clear\" />");
                sb.Append("</li>");
                sb.Append("<li>" + model.Content + "</li>");
                sb.Append("<li class=\"replybox\"><a href=\"#\">回复</a>");
                sb.Append("<div class=\"clearfix\">");
                sb.Append("<textarea id=\"txt" + model.AppraisalID.ToString() + "\"></textarea>");
                sb.Append("<a href=\"javascript:ReplyAppraisal(" + model.AppraisalID.ToString() + ")\">提交回复</a>");
                sb.Append("</div>");
                sb.Append("</li>");
                sb.Append("<li class=\"replylist\" id=\"rl" + model.AppraisalID + "\">");
                IList<HotelDetail_ReviewsReply_Model> list = listReviewsReply.Where(p => p.ReviewsID == model.AppraisalID).ToList();
                foreach (HotelDetail_ReviewsReply_Model m in list)
                {
                    sb.Append("<div class=\"replyitem\">");
                    sb.Append("<p>");
                    sb.Append(m.ReplyContent);
                    sb.Append("<br/>");
                    sb.Append(m.LoginName + " " + m.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                    sb.Append("</p>");
                    sb.Append("</div>");
                }
                sb.Append("</li>");
                sb.Append("</ul>");
                sb.Append("</div>");
            }

            int num = Convert.ToInt32(total);
            int page = num % 3 == 0 ? num / 3 : num / 3 + 1;

            return score.Replace(".0", "") + "/5," + page.ToString() + "," + sb.ToString();
        }

        /// <summary>
        /// 根据酒店ID评价
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string HotelsAppraisal(int hotelid, string location, string facilities, string service, string health, string content)
        {
            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel ubm = ((UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL]);
                AppraisalDac dac = new AppraisalDac();
                AppraisalModel model = new AppraisalModel();
                model.HotelsID = hotelid;
                model.UserID = ubm.ID;
                model.Content = content;
                model.AppraisalTime = DateTime.Now;
                model.Location = ConvertAppraisal(location);
                model.Facilities = ConvertAppraisal(facilities);
                model.Service = ConvertAppraisal(service);
                model.Health = ConvertAppraisal(health);
                model.CheckStatus = 0;
                model.IsDeleted = 0;
                model.Created = ubm.ID;
                model.CreateTime = DateTime.Now;
                model.Updated = ubm.ID;
                model.UpdateTime = DateTime.Now;
                int result = dac.Add(model);
                return result == -1 ? "-1" : "1";
            }
            else
            {
                return "0";
            }
        }

        /// <summary>
        /// 评价字符串转数字 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static int ConvertAppraisal(string s)
        {
            switch (s)
            {
                case "不满意": return 1;
                case "一般": return 2;
                case "中等": return 3;
                case "满意": return 4;
                case "非常满意": return 5;
                default: return 5;
            }
        }

        /// <summary>
        /// 回复评价
        /// </summary>
        /// <param name="hotelid"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        [WebMethod]
        public static string ReplyAppraisal(int appaisalid, string content)
        {
            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel ubm = ((UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL]);
                ReviewsReplyDac dac = new ReviewsReplyDac();
                int result = dac.ReplyAppraisal(appaisalid, content, ubm.ID);
                if (result == -1)
                {
                    return "-1";
                }
                else
                {
                    string str = "<div class=\"replyitem\"><p>" + content + "<br/>" + ubm.LoginName + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</p></div>";
                    return str;
                }
            }
            else
            {
                return "0";
            }
        }
    }
}